/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: WxCateLogServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/31 12:05
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.vo.CategoryAllInfoVo;
import com.cskaoyan.bean.vo.WxCateLogVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxCateLogServiceImpl implements WxCateLogService{
    @Autowired
    MarketCategoryMapper CategoryMapper;
    /*
    * 根据id查询出当前顶级目录的而信息 以及包含的子目录的信息集合
    * */
    @Override
    public WxCateLogVo getCateLogInfo(Integer id) {
        //根据id查出父目录
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketCategory> categoriesFaList = CategoryMapper.selectByExample(marketCategoryExample);
        MarketCategory marketCategoryFa = categoriesFaList.get(0);
        //根据父级目录的id作为子目录的pid查询
        marketCategoryExample.clear();
        marketCategoryExample.createCriteria().andPidEqualTo(marketCategoryFa.getId());
        //查询所有的子目录
        List<MarketCategory> categoriesSonList=CategoryMapper.selectByExample(marketCategoryExample);

        WxCateLogVo wxCateLogVo = new WxCateLogVo();
        wxCateLogVo.setCurrentCategory(marketCategoryFa);
        wxCateLogVo.setCurrentSubCategory(categoriesSonList);
        return wxCateLogVo;
    }
/*
*
* 页面的回显 获得所有的category信息 父目录以及子目录
* */
    @Override
    public CategoryAllInfoVo getCategoryAllInfo() {
        //先查出所有的父目录
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        //以及目录的pid为0
        marketCategoryExample.createCriteria().andPidEqualTo(0);
        List<MarketCategory> FaCategories = CategoryMapper.selectByExample(marketCategoryExample);
        //获取刷新页面后的默认目录
        MarketCategory currentCategory = FaCategories.get(0);
        //获取当前父目录下的所有的内容
        marketCategoryExample.clear();
        marketCategoryExample.createCriteria().andPidEqualTo(currentCategory.getPid());
        List<MarketCategory> SonCategories = CategoryMapper.selectByExample(marketCategoryExample);
        CategoryAllInfoVo categoryAllInfoVo = new CategoryAllInfoVo(FaCategories, currentCategory, SonCategories);
        return categoryAllInfoVo;
    }

}
